// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import { AlertPlugin, ToastPlugin } from 'vux'
import vueResource from 'vue-resource'
Vue.use(vueResource);

Vue.use(AlertPlugin)
Vue.use(ToastPlugin)
Vue.config.productionTip = false

/* eslint-disable no-new */

Vue.prototype.Ajax=function(method,url,data,getData,){          //封装vue-resource
  let head=''
  if(method=='post'){
    this.$http.post(head+url,data).then((res)=>{
      if(res.body.body!=='success'&&res.body.sc=='200'){
        res=JSON.parse(res.body.body)
        getData(res)
      }else{
        getData(res)
      }
    },()=>{
      getData("110")      //响应失败
    })}
  if(method=='get'){
    this.$http.get(head+url,{params:data}).then((res)=>{
      if(res.body.body!=='success'&&res.body.sc=='200'){
        try{res=JSON.parse(res.body.body)}catch(err){console.log(err)}
        getData(res)
      }else{
        getData(res)
      }
    },()=>{
      getData("110")      //响应失败
    })
  }
  if(method=='delete'){
    console.log(data)
    this.$http.delete(head+url,{body:data}).then((res)=>{
      getData(res)
    })
  }
  if(method=='put'){
    this.$http.put(head+url,data).then((res)=>{

      getData(res)
    },()=>{
      getData("110")      //响应失败
    })}
}
Vue.prototype.setCookie = function(c_name,value,expiredays) {    //设置cookie
  var exdate=new Date()
  exdate.setDate(exdate.getDate()+expiredays)
  document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
};
Vue.prototype.getCookie=function(c_name) {             //取出cookie
  if (document.cookie.length>0)
  {
    var  c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1)
    {
      c_start=c_start + c_name.length+1
      var c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
    }
  }
  return ""
};
Vue.http.interceptors.push((request, next) => {   //http拦截器，在请求中添加token
  let token=Vue.prototype.getCookie('ttf-token')
  request.headers.set('ttf-token', token)
  next(response => {
    return response
  })
})
new Vue({
  el: '#app',
  router,
  components: { App },
  template: '<App/>'
})
